Predict with sims from density and diet models to propagate uncertainty
# Predict and then for loop to summarise and avoid having too large pred grids in the memorynsim <-500sim_dens <-predict(mcod, newdata = pred_grid, nsim = nsim) |>as.data.frame()sim_her <-predict(mher, newdata = pred_grid, nsim = nsim) |>as.data.frame() |>bind_cols(pred_grid |> dplyr::select(X, Y, year))sim_spr <-predict(mspr, newdata = pred_grid, nsim = nsim) |>as.data.frame() |>bind_cols(pred_grid |> dplyr::select(X, Y, year))sim_sad <-predict(msad, newdata = pred_grid, nsim = nsim) |>as.data.frame() |>bind_cols(pred_grid |> dplyr::select(X, Y, year))
The density prediction is used later for spatial overlap + it gets left_joined to the other sims, so it gets a special treatment
pred_dens_space <- pred_grid_density |>filter(year %in%c(1994, 2022)) |>summarise(density_mean =mean(sim_density),.by =c(year, X, Y) )annotations <-data.frame(xpos =c(-Inf, -Inf),ypos =c(-Inf, Inf),year =c(1994, 2022),hjust =c(-0.3, -0.3),vjust =c(-29.2, 1.5))# Max density by species (because I trim the plot)pred_dens_space |>summarise(max =max(density_mean))
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
ggsave(paste0(home, "/figures/spatial_cod.pdf"), width =17, height =9, units ="cm")
Plot annual predation with uncertainty
# Summarise predator biomass by year and sim and join that to calculate per capita predationpred_dens <- pred_grid_density |>summarise(cod_biomass =sum(sim_density *9), # area is 9 km2.by =c(year, sim) )# Get the predation by year in a loop, else it becomes too long with all the sims, running out of memory...herlist <-list()sadlist <-list()sprlist <-list()tic()for (i inunique(pred_grid$year)) { sim_her_sub <- sim_her |>filter(year == i) sim_sad_sub <- sim_sad |>filter(year == i) sim_spr_sub <- sim_spr |>filter(year == i) pred_grid_density_sub <- pred_grid_density |>filter(year == i) herlist[[i]] <-get_annual_predation(sim_her_sub, prey_name ="Herring") sadlist[[i]] <-get_annual_predation(sim_sad_sub, prey_name ="Saduria") sprlist[[i]] <-get_annual_predation(sim_spr_sub, prey_name ="Sprat")gc()}tot_pred <-bind_rows(bind_rows(herlist),bind_rows(sadlist),bind_rows(sprlist))toc()
304.477 sec elapsed
# Prepare data for fitting gams and for plottingclean_pred <- tot_pred |>mutate(species =as.factor(species))# Fit gamma-GAMs to annual predation metricsm_pop <-brm( pred_median ~ species +s(year, by = species),family =Gamma(link ="log"),control =list(adapt_delta =0.99),data = clean_pred |>mutate(pred_median = pred_median /1000))
Scale for fill is already present.
Adding another scale for fill, which will replace the existing scale.
tag_facet(p1, fontface =1, col ="gray30")
ggsave(paste0(home, "/figures/supp/cv_density_relative_prey.pdf"), width =19, height =10, units ="cm")
Plot relative prey weight predictions in space for two years
# Here we can use the get_cv_predation and just use the median# Max predation by species (if I trim the plot)cv_pred |>summarise(max =max(mean), .by = species)
# A tibble: 3 × 2
species max
<chr> <dbl>
1 Herring 0.00721
2 Saduria 0.0266
3 Sprat 0.0406
# Plot!viridis(n =3, option ="magma") # high color